
#include "graphicscore.h"

#include <math.h>

static BOOL init = FALSE;

void GraphicsCore_Init(int width,int height)
{
	OpenGL_Init(width,height);
	#ifdef _WIN32
		init = FALSE;
	#endif
	if (!init)
	{
		TiledSprite_Init(&flareSprites,SPRITE_FLARE_FILENAME,128,128,512,512);
		TiledSprite_Init(&smokeSprites,SPRITE_SMOKE_FILENAME,171,171,1024,1024);
		Cubemap_Init(CUBEMAP_TEXTURE_FILENAME);
		Land_Init(LAND_HEIGHTMAP_TEXTURE_FILENAME, LAND_TEXTURE_FILENAME);
		init = TRUE;
	}
}

void GraphicsCore_InitFrame()
{
	OpenGL_InitFrame();
}

void GraphicsCore_Destroy()
{
	TiledSprite_Destroy(&flareSprites);
	TiledSprite_Destroy(&smokeSprites);
	Cubemap_Destroy();
	Land_Destoy();
}

void GraphicsCore_EndFrame()
{
	OpenGL_EndFrame();
}

void GraphicsCore_DrawParticle(const Vector3f* position, const Camera* camera)
{
	Sprite s;
	Color c = {255,255,255,255};
	TiledSprite_GetSprite(&flareSprites,&s,position,&c,camera->theta,camera->phi, PARTICULE_SPRITE_FLARE );
	Sprite_Draw(&s,camera,1.0f);
}

void GraphicsCore_DrawAxis()
{
	Vector3f dots[6] = {{0,0,0},
						{2,0,0},
						{0,0,0},
						{0,2,0},
						{0,0,0},
						{0,0,2}};
	Color colors[3] = {{255,0,0,255},
					   {0,255,0,255},
					   {0,0,255,255}};
	int lineWidhts[3] = {2,2,2};
	OpenGL_DrawLines(dots,colors,lineWidhts,3,LINES_DRAW_MODE_SEPARATE);
}

void GraphicsCore_DrawMode(int mode)
{
	OpenGL_DrawMode(mode);
}

void GraphicsCore_DrawCubemap(const Camera* camera)
{
	Cubemap_Draw(camera);
}

void GraphicsCore_DrawLand(const Camera* camera, float elapsed)
{
	Land_Draw(camera, elapsed);
}

void GraphicsCore_UpdateCamera(const Camera* camera)
{
	Vector3f targetVec = Camera_GetTarget(camera);
	OpenGL_LookAt(&camera->position, &targetVec);
}

unsigned int GraphicsCore_LoadTexture(const char * filename,BOOL useMipMap)
{
	return Texture_LoadTexture(filename,useMipMap);
}
